﻿using UnityEditor;
using UnityEngine;

namespace UFrame
{
    public class PLReplaceEditorWindow : EditorWindow
    {
        private GameObject selectGo;

        [MenuItem("Plateface/ShortKey/替换选中GameObject")]
        public static void Open()
        {
            var win = GetWindow<PLReplaceEditorWindow>();
            win.titleContent = new GUIContent("存档界面");
        }

        public void OnGUI()
        {
            selectGo = (GameObject)EditorGUILayout.ObjectField("替换的预制体", selectGo, typeof(GameObject), true);

            if (GUILayout.Button("修改"))
            {
                var objAry = Selection.objects;
                if (objAry == null || objAry.Length == 0)
                {
                    //EditorGUILayout.HelpBox(new GUIContent("请选中场景中的物体"),true);
                    Debug.Log("请选中场景中的物体");
                    return;
                }

                if (selectGo == null)
                {
                    return;
                }

                foreach (var o in objAry)
                {
                    GameObject go = (GameObject)o;
                    Transform t = go.GetComponent<Transform>();
                    var a = Instantiate(selectGo);
                    a.transform.SetParent(go.transform.parent);
                    a.transform.position = go.transform.position;
                    a.transform.rotation = go.transform.rotation;
                    a.transform.localScale = go.transform.localScale;
                }


                /*
                for (int i = 0; i < objAry.Length; i++)
                {
                    Destroy(objAry[i]);
                }
                */
            }
        }
    }
}